/**
 * authentication.js
 * 鉴权action, 描述鉴权相关的用户行为
 *
 * created by qiuwei
 * version 1.0.0
 * 2016年03月07日
 */

import { create } from './asyncActionCreator';
import { AUTHENTICATE_URL } from '../../../constants/API';


/**
 * 失去焦点时发生错误
 * @type {String}
 */
export const SHOW_ERROR = 'error on blur';
export const showError = (errorMessage) => ({
	type: SHOW_ERROR,
	payload: {
		errorMessage
	}
});

/**
 * 设置是否自动登录
 * @type {String}
 */
export const AUTO_AUTHENTICATE = 'set auto authenticate';
export const setAutoAuthenticate = (enableOrDisable) => ({
	type: AUTO_AUTHENTICATE,
	payload: {
		enableOrDisable
	}
});

/**
 * 异步验证用户信息
 * @type {String}
 */
export const AUTHENTICATE_REQUEST = 'authenticate request',
			 AUTHENTICATE_SUCCESS = 'authenticate success',
			 AUTHENTICATE_FAILURE = 'authenticate failure';

export const authenticateRequest = () => ({
	type: AUTHENTICATE_REQUEST,
	payload: {}
});

export const authenticateSuccess = (user) => ({
	type: AUTHENTICATE_SUCCESS,
	payload: {
		user
	}
});

export const authenticateFailure = (errorMessage) => ({
	type: AUTHENTICATE_FAILURE,
	payload: {
		errorMessage
	}
});

export const authenticate = (loginInfo, onSuccess) => {

	let params = {
		mobile: loginInfo.userName,
		password: loginInfo.password,
		security_code: loginInfo.securityCode
	};

	return create(AUTHENTICATE_URL,
		authenticateRequest,
		authenticateSuccess,
		authenticateFailure,
		params,
		onSuccess);
};

/**
 * 用户退出
 * @type {String}
 */
export const LOGOUT = 'current user logout';
export const logout = () => ({
	type: LOGOUT,
	payload: {}
});








